package cn.gok.controller;

import cn.gok.entity.Student;
import cn.gok.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    //查询所有学生的信息
    @GetMapping
    public List<Student> getAll() {
        return studentService.list(null);
    }

    //根据id查询所有学生信息
    @GetMapping("{sid}")
    public Student getStudentById(@PathVariable Long sid) {
        return studentService.getById(sid);
    }

    //模糊查询学生信息
    @GetMapping("/like")
    public List<Student> getLikeStudentInfo(@RequestBody Student student) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper
                .like("sname", student.getSname());
        return studentService.list(wrapper);
    }
    //分页查询，getPage方法在对应的mapper和service层
    // http://localhost:8080/student/1/2
    @GetMapping("{currentPage}/{pageSize}")
    public IPage<Student> getPage(@PathVariable int currentPage, @PathVariable int pageSize) {

        return studentService.getPage(currentPage, pageSize);
    }
    //添加班级
    @PostMapping
    public boolean saveStudent(@RequestBody Student student){
        return studentService.save(student);
    }
    //修改班级信息
    @PutMapping
    public boolean updateStudent(@RequestBody Student student){
        return  studentService.updateById(student);
    }
    //根据id删除班级
    @DeleteMapping("{sid}")
    public boolean deleteByStudent(@PathVariable Long sid){
        return studentService.removeById(sid);
    }
    //批量删除班级
    @DeleteMapping
    public boolean deleteStudentByIds(@RequestBody List<Integer> sids){
        return studentService.removeByIds(sids);
    }
}
